package com.my.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.my.R;
import com.my.util.ApiUtil;

public class UserInfoActivity extends AppCompatActivity {

    SharedPreferences preferences = null;
    EditText name;
    EditText password;
    EditText phone;
    TextView addressText;
    EditText address;
    RadioButton sex1;
    RadioButton sex0;
    EditText major;
    Button submit;
    LinearLayout ll5;

    Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            JSONObject rest = ((JSONObject) msg.obj);
            if(rest.getInt("code") == 200) {
                Toast.makeText(UserInfoActivity.this, "修改成功！", Toast.LENGTH_LONG).show();
                preferences.edit().putString("user", rest.getJSONObject("data").getStr("user")).commit();
                onBackPressed();
            }else {
                Toast.makeText(UserInfoActivity.this, rest.getStr("msg"), Toast.LENGTH_LONG).show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowCustomEnabled(true);
        }
        preferences = this.getSharedPreferences("LOGIN_DATA", Context.MODE_PRIVATE);
        JSONObject user = new JSONObject(preferences.getString("user", null));
        name = findViewById(R.id.name);
        password = findViewById(R.id.password);
        phone = findViewById(R.id.phone);
        addressText = findViewById(R.id.address_text);
        address = findViewById(R.id.address);
        sex1 = findViewById(R.id.sex1);
        sex0 = findViewById(R.id.sex0);
        major = findViewById(R.id.major);
        submit = findViewById(R.id.submit);
        ll5 = findViewById(R.id.ll5);
        if(user.getInt("type") != 3) {
            addressText.setText("办公室：");
            major.setVisibility(View.GONE);
        }
        name.setText(user.getStr("name"));
        phone.setText(user.getStr("phone"));
        address.setText(user.getStr("address"));
        sex1.setChecked(user.getInt("sex") == 1);
        sex0.setChecked(user.getInt("sex") == 0);
        if(user.getInt("type") == 3) {
            major.setText(user.getStr("major"));
            ll5.setVisibility(View.VISIBLE);
        }
        submit.setOnClickListener(v -> {
            if(StrUtil.isBlank(name.getText())) {
                Toast.makeText(this, "姓名不能为空！", Toast.LENGTH_LONG).show();
                return;
            }
            if(StrUtil.isBlank(phone.getText())) {
                Toast.makeText(this, "联系方式不能为空！", Toast.LENGTH_LONG).show();
                return;
            }
            if(StrUtil.isBlank(address.getText())) {
                Toast.makeText(this, addressText.getText() + "不能为空！", Toast.LENGTH_LONG).show();
                return;
            }
            if(StrUtil.isBlank(major.getText()) && user.getInt("type") == 3) {
                Toast.makeText(this, "专业不能为空！", Toast.LENGTH_LONG).show();
                return;
            }
            JSONObject params = new JSONObject().putOpt("name", name.getText()).putOpt("password", password.getText())
                    .putOpt("phone", phone.getText()).putOpt("address", address.getText()).putOpt("major", major.getText())
                    .putOpt("sex", sex1.isChecked() ? 1 : 0).putOpt("id", user.getLong("id"));
            new Thread(() -> {
                Message message = new Message();
                message.obj = JSONUtil.parseObj(HttpUtil.post(ApiUtil.HTTP_ADDRESS + "user/update", params.toString()));
                handler.sendMessage(message);
            }).start();
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == 16908332) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

}